package com.centersoft.flowable.db.model;

import com.centersoft.flowable.core.model.FlowModelModel;
import com.centersoft.flowable.db.model.entity.FlowModelEntity;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Optional;

/**
 * @author wangzhiqiang
 * @since 2025/4/5 17:59
 */
public class FlowModelConvert {
    private static final ZoneId DEFAULT_ZONE = ZoneId.systemDefault();

    public static FlowModelModel toModel(FlowModelEntity entity) {
        FlowModelModel flowModelModel = new FlowModelModel();
        flowModelModel.setId(entity.getId());
        flowModelModel.setName(entity.getName());
        flowModelModel.setKey(entity.getModelKey());
        flowModelModel.setDescription(entity.getDescription());
        flowModelModel.setCreated(parseDate(entity.getCreated()));
        flowModelModel.setLastUpdated(parseDate(entity.getLastUpdated()));
        flowModelModel.setCreatedBy(entity.getCreatedBy());
        flowModelModel.setLastUpdatedBy(entity.getLastUpdatedBy());
        flowModelModel.setVersion(entity.getVersion());
        flowModelModel.setModelEditorJson(entity.getModelEditorJson());
        flowModelModel.setComment(entity.getModelComment());
        flowModelModel.setModelType(entity.getModelType());
        flowModelModel.setTenantId(entity.getTenantId());
        flowModelModel.setThumbnail(entity.getThumbnail());
        flowModelModel.setOrgCode(entity.getOrgCode());
        return flowModelModel;

    }
    public static Date  parseDate(LocalDateTime localDateTime ){
        return Optional.ofNullable(localDateTime)
                .map(ldt -> ldt.atZone(DEFAULT_ZONE).toInstant())
                .map(Date::from)
                .orElse(null);
    }
}
